package com.minke.ch11springbootinterceptor.controller;

import com.minke.ch11springbootinterceptor.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 何杨谊
 * @description: TODO
 * @date 2022/4/3 22:45
 */
@RestController()
@RequestMapping("/api/user")
public class UserController {

    @GetMapping("/open/login")
    public Object login(HttpServletRequest request) {
        User user = new User();
        user.setId(1);
        user.setName("小何同学");
        request.getSession().setAttribute("user", user);
        return user;
    }

    @GetMapping("/open/logout")
    public Object logout(HttpServletRequest request){
        request.getSession().setAttribute("user",null);
        return "登出成功";
    }

    @GetMapping("/info")
    public Object getInfo(HttpServletRequest request){
       User user=(User) request.getSession().getAttribute("user");
       return user;
    }

    @GetMapping("/open/no-login")
    public Object noLogin(){
        return "需要登录";
    }
}
